package project02.view;

import com.sun.javafx.collections.MappingChange;
import com.sun.org.apache.xalan.internal.utils.XMLSecurityManager;
import com.sun.org.apache.xalan.internal.xsltc.util.IntegerArray;
import project02.domain.User;
import project02.service.UserListService;
import sun.security.util.Password;

import java.io.*;
import java.util.*;

public class TSUtility {
    private static Scanner scanner = new Scanner(System.in);

    public static String inputCheck(int minLength, int maxLength, String reg, String tips) {
        String receiveOrigin = null;
        System.out.println("结束本次输入按空格回车即可。");
        do {
            receiveOrigin = scanner.next();
            if (" ".equals(receiveOrigin)) {
                return null;
            }
            if (receiveOrigin.length() < minLength) {
                System.out.println("输入长度过短，请重新输入。");
                continue;
            }
            if (receiveOrigin.length() > maxLength) {
                System.out.println("输入长度过长，请重新输入。");
                continue;
            }
            if (reg != null) {
                if (!receiveOrigin.matches(reg)) {
                    if (null != tips) {
                        System.out.println(tips);
                    } else {
                        System.out.println("输入格式有误");
                    }

                    continue;
                }
            }
            break;
        } while (true);
        return receiveOrigin;
    }

    public static void main(String[] args) {
        System.out.println("请输入你的学院：");
        String department = null;
        do {
            department = TSUtility.inputCheck(3, 20);
            if (!department.endsWith("学院")) {
                System.out.println("您不知道您的学院吗？滚回去找导师扣分！");

            }
        } while (!department.endsWith("学院"));
        System.out.println("请输入你的电话：");
    }

    public static char readInitMenuSelection() {
        char c;
        for (; ; ) {
            String str = readKeyBoard(1, false);
            c = str.charAt(0);
            if (c != '1' && c != '2' &&
                    c != '3') {
                System.out.print("选择错误，请重新输入：");
            } else break;
        }
        return c;
    }

    public static void readFile() throws FileNotFoundException {
        BufferedReader bis = new BufferedReader(new FileReader(UserListService.INFORMATION));
    }

    public static char readMenuSelection() {
        char c;
        for (; ; ) {
            String str = readKeyBoard(1, false);
            c = str.charAt(0);
            if (c != '1' && c != '2' &&
                    c != '3' && c != '4' && c != '5' && c != '6' &&
                    c != '7' && c != '8' && c != '9') {
                System.out.print("选择错误，请重新输入：");
            } else break;
        }
        return c;
    }

    public static char readMenuSelectionPro() {
        char c;
        for (; ; ) {
            String str = readKeyBoard(1, false);
            c = str.charAt(0);
            if (c != '1' && c != '2' &&
                    c != '3' && c != '4' && c != '5' && c != '6' &&
                    c != '7') {
                System.out.print("选择错误，请重新输入：");
            } else break;
        }
        return c;
    }


    public static void readReturn() {
        System.out.print("按回车键继续...");
        readKeyBoard(100, true);
    }

    public static int readInt() {
        int n;
        for (; ; ) {
            String str = readKeyBoard(2, false);
            try {
                n = Integer.parseInt(str);
                break;
            } catch (NumberFormatException e) {
                System.out.print("数字输入错误，请重新输入：");
            }
        }
        return n;
    }

    public static int readStock() {
        int n;
        for (; ; ) {
            String str = readKeyBoard(6, false);
            try {
                n = Integer.parseInt(str);
                break;
            } catch (NumberFormatException e) {
                System.out.print("数字输入错误，请重新输入：");
            }
        }
        return n;
    }

    public static Double readDouble() {
        Double n;
        for (; ; ) {
            String str = readKeyBoard(6, false);
            try {
                n = Double.parseDouble(str);
                break;
            } catch (NumberFormatException e) {
                System.out.print("数字输入错误，请重新输入：");
            }
        }
        return n;
    }

    public static char readConfirmSelection() {
        char c;
        for (; ; ) {
            String str = readKeyBoard(1, false).toUpperCase();
            c = str.charAt(0);
            if (c == 'Y' || c == 'N') {
                break;
            } else {
                System.out.print("选择错误，请重新输入：");
            }
        }
        return c;
    }

    public static String readString(int limit, String defaultValue) {
        String str = readKeyBoard(limit, true);
        return str.equals("") ? defaultValue : str;
    }

    public static String readKeyBoard(int limit, boolean blankReturn) {
        String line = "";

        while (scanner.hasNextLine()) {
            line = scanner.nextLine();
            if (line.length() == 0) {
                if (blankReturn) return line;
                else continue;
            }

            if (line.length() < 1 || line.length() > limit) {
                System.out.print("输入长度（不大于" + limit + "）错误，请重新输入：");
                continue;
            }
            break;
        }

        return line;
    }

    public static void loadSpecialEffects() throws InterruptedException {
        System.out.println("请稍等：");
        for (int i1 = 1; i1 <= 100; i1++) {
            System.out.print("加载中：" + i1 + "%");
            Thread.sleep(new Random().nextInt(25) + 1);
            if (i1 == 100) {
                Thread.sleep(50);
            }
            System.out.print("\r");
        }
    }

    public static Integer readInt(int i, Integer age) {
        int n;
        for (; ; ) {
            String str = readKeyBoard(2, false);
            try {
                n = Integer.parseInt(str);
                break;
            } catch (NumberFormatException e) {
                System.out.print("数字输入错误，请重新输入：");
            }
        }
        return n;
    }

    public static String inputCheck(int minLength, int maxLength) {
        return inputCheck(minLength, maxLength, null, null);


    }

//
}